package com.jrwd.recycletest;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jrwd.recycletest.util.DensityUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 小兵哥哥 on 2018/8/22.
 */

public class login extends AppCompatActivity {

    @BindView(R.id.sousuo)
    LinearLayout sousuo;
    @BindView(R.id.tab_screen)
    TabLayout tabScreen;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.start)
    ImageView start;
    private FrameLayout.LayoutParams params;
    private FrameLayout.LayoutParams params1;
    private FrameLayout.LayoutParams params2;
    @BindView(R.id.edittext_username)
    EditText edittextUsername;
    @BindView(R.id.text_username)
    TextInputLayout textUsername;
    @BindView(R.id.edittext_password)
    EditText edittextPassword;
    @BindView(R.id.text_password)
    TextInputLayout textPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.imageview_back)
    ImageView imageviewBack;
    @BindView(R.id.collapsingtoolbar)
    CollapsingToolbarLayout collapsingtoolbar;
    @BindView(R.id.appbarlayout)
    AppBarLayout appbarlayout;

    @BindView(R.id.coordinatorlayout)
    CoordinatorLayout coordinatorlayout;
    private List<String> screenList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ButterKnife.bind(this);
        initt();
        textUsername.setHint("    用户名/Email/手机号");
        textPassword.setHint("    密码");
        edittextUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                username();
            }
        });
        edittextPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                password();
            }
        });

        appbarlayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                params = (FrameLayout.LayoutParams) sousuo.getLayoutParams();
//                params.height = -verticalOffset/6;
                params.setMargins(-verticalOffset, 0, -verticalOffset / 3, 0);
                sousuo.requestLayout();

              //  params1 = (FrameLayout.LayoutParams) start.getLayoutParams();
                start.setAlpha((float)-verticalOffset/432);
              //  start.requestLayout();

                Log.e("adsf", String.valueOf(verticalOffset));
            }
        });
    }

    private void initt() {
        screenList = new ArrayList<>();
        for (int i = 0; i < 8; i++)
            screenList.add("第" + i + "个");
        initTab();
        initviewPager();
    }

    class TabAdapter extends FragmentPagerAdapter {

        public TabAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            ProListFrag fragment = new ProListFrag();
            Bundle bundle = new Bundle();
            bundle.putInt("position", position);
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public int getCount() {
            return screenList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

        }

        //设置tablayout标题，方法继承自PagerAdapter，默认是返回null，所以不重写这个方法tab就没字了
        @Override
        public CharSequence getPageTitle(int position) {
            return screenList.get(position);

        }
    }

    private void initviewPager() {

        viewPager.setAdapter(new TabAdapter(getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(screenList.size());
        tabScreen.setupWithViewPager(viewPager);
        //设置可以滑动
        tabScreen.setTabMode(TabLayout.MODE_SCROLLABLE);
        viewPager.setCurrentItem(2);
    }

    private void initTab() {
        RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 24, 24);//设置边距
        for (int i = 0; i < screenList.size(); i++) {
            tabScreen.addTab(tabScreen.newTab().setText(screenList.get(i)));
        }
        tabScreen.getTabAt(2).select();
        reflex(tabScreen);
    }

    public void reflex(final TabLayout tabLayout) {
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp15 = DensityUtils.dp2px(tabLayout.getContext(), 15);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);
                        TextView mTextView = (TextView) mTextViewField.get(tabView);
                        tabView.setPadding(0, 0, 0, 0);
                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为30dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp15;
                        params.rightMargin = dp15;
                        tabView.setLayoutParams(params);
                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private boolean username() {
        if (TextUtils.isEmpty(edittextUsername.getText())) {
            textUsername.setError("用户名/Email/手机号不能为空");
            return false;
        } else {
            textUsername.setError(null);
        }
        return true;
    }

    private boolean password() {
        if (TextUtils.isEmpty(edittextPassword.getText())) {
            textPassword.setError("密码不能为空");
            return false;
        } else {
            textPassword.setError(null);
        }
        return true;
    }
}

